<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>商品列表</title>
    <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/layui@2.6.8/dist/css/layui.css">
    <script src="//cdn.jsdelivr.net/npm/layui@2.6.8/dist/layui.js" charset="utf-8"></script>
</head>

<body>
    <div style="margin: 15px;padding: 15px;background-color: #fff;">
        <div class="layui-form" style="border-bottom:1px solid #f6f6f6">
            <div class="layui-form-item">
                <!-- 搜索字段 -->
                <div class="layui-inline">
                    <label class="layui-form-label">商品名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="title" value="" placeholder="请输入" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">品牌</label>
                    <div class="layui-input-block">
                        <select name="brand_id" lay-search="">
                            <option value=""></option>
                            {foreach $brands as $brand}
                            <option value="{$brand['id']}">{$brand['title']}</option>
                            {/foreach}
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button type="submit" lay-submit lay-filter="search" class="layui-btn">
                        <i class="layui-icon layui-icon-search"></i>
                    </button>
                </div>
            </div>
        </div>

        <table class="layui-table"
            lay-data="{'url':'/admins/products/index', where:{search: ''}, id: 'productTable', toolbar: '#toolbar', page:true}"
            lay-filter="product">
            <thead>
                <tr>
                    <th lay-data="{checkbox:true, fixed: 'left'}"></th>
                    <th lay-data="{field:'id', width:80, sort: true}">ID</th>
                    <th lay-data="{field:'title', edit: true}">商品名称</th>
                    <th lay-data="{field:'img', sort: true}">缩略图</th>
                    <th lay-data="{field:'brand_title'}">品牌</th>
                    <th lay-data="{field:'orig_price', sort: true}">原价</th>
                    <th lay-data="{field:'min_price', sort: true}">最低价</th>
                    <th lay-data="{width:170, align:'center', toolbar: '#tool'}">操作</th>
                </tr>
            </thead>
        </table>
    </div>

    <script type="text/html" id="toolbar">
        <div class="layui-btn-group">
            <button class="layui-btn layui-btn-sm" lay-event="delAll">批量删除</button>
            <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
            <button class="layui-btn layui-btn-sm" lay-event="refresh"><i class="layui-icon layui-icon-refresh"></i></button>
        </div>
    </script>

    <script type="text/html" id="tool">
        <a class="layui-btn layui-btn layui-btn-xs layui-btn-primary" lay-event="skus">规格</a>
        <a class="layui-btn layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

    <script>
        layui.use(['table', 'form'], function () {
            var table = layui.table
                , form = layui.form
                , $ = layui.jquery;

            //获取应用、控制器和方法
            var module = '{:app("http")->getName()}'
                , controller = '{$Request.controller}'
                , action = '{$Request.action}';

            //头工具栏事件
            table.on('toolbar(product)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id); //idTest 即为基础参数 id 对应的值
                var data = checkStatus.data; //获取选中行的数据
                var length = checkStatus.data.length; //获取选中行数量，可作为是否有选中行的条件
                var checkAll = checkStatus.isAll; //表格是否全选
                var layEvent = obj.event; //头部工具栏事件

                //头工具栏批量删除
                if (layEvent === 'delAll') {
                    if (data.length === 0) return layer.msg('请选择数据');
                    layer.confirm('确定删除吗？', { icon: 3 }, function (index) {
                        var id = [];
                        data.forEach(elem => id.push(elem.id));
                        $.post('/' + module + '/' + controller + '/del', { id }, function (res) {
                            if (res.code) return layer.msg(res.msg, { icon: 2 });
                            layer.msg(res.msg);
                            table.reload(obj.config.id);
                        }, 'json');
                    });
                }

                //头工具栏添加
                if (layEvent === 'add') {
                    layer.open({
                        type: 2,
                        maxmin: true,
                        title: '添加',
                        shadeClose: true,
                        shade: 0.3,
                        area: $(window).width() <= 480 ? ['95%', '80%'] : ['480px', '450px'],
                        content: '/' + module + '/' + controller + '/add',
                    });
                }

                //头工具栏刷新
                if (layEvent === 'refresh') {
                    table.reload(obj.config.id);
                }
            });

            //行工具事件
            table.on('tool(product)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
                var data = obj.data; //获得当前行数据
                var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
                var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）

                //行工具查看
                if (layEvent === 'detail') {
                    //do somehing
                }

                //行工具删除
                if (layEvent === 'del') {
                    layer.confirm('真的删除行么？', { icon: 3 }, function (index) {
                        obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                        layer.close(index);
                        //向服务端发送删除指令
                        $.post('/' + module + '/' + controller + '/del', { id: data.id }, function (res) {
                            if (res.code) return layer.msg(res.msg, { icon: 2 });
                            layer.msg(res.msg);
                        }, 'json');
                    });
                }

                //行工具编辑
                if (layEvent === 'edit') {
                    layer.open({
                        type: 2,
                        maxmin: true,
                        title: '编辑',
                        shadeClose: true,
                        shade: 0.3,
                        area: $(window).width() <= 480 ? ['95%', '80%'] : ['480px', '450px'],
                        content: '/' + module + '/' + controller + '/edit?id=' + data.id,
                    });
                    //同步更新缓存对应的值
                    // obj.update({
                    //     username: '123'
                    //     , title: 'xxx'
                    // });
                }

                //规格
                if (layEvent === 'skus') {
                    window.location.href = '/' + module + '/' + controller + '/skus?pro_id='+ data.id;
                }

                //行工具右侧扩展
                if (layEvent === 'LAYTABLE_TIPS') {
                    layer.alert('Hi，头部工具栏扩展的右侧图标。');
                }
            });

            //单元格编辑
            table.on('edit(product)', function (obj) {
                var value = obj.value //得到修改后的值
                    , data = obj.data //得到所在行所有键值
                    , field = obj.field; //得到字段
                var datas = {};
                datas['id'] = data.id;
                datas[field] = util.escape(value);
                $.post('/' + module + '/' + controller + '/modify', datas, function (res) {
                    if (res.code) {
                        layer.msg(res.msg, { icon: 2 });
                        obj.update(data);
                        return;
                    }
                    layer.msg(res.msg);
                }, 'json');
            });

            //搜索框事件
            form.on('submit(search)', function (obj) {
                //执行重载
                table.reload('productTable', {
                    where: {
                        search: obj.field
                    }
                });
                return false;
            });
        });
    </script>

</body>

</html>